/*
 * Copyright (c) novafaen design 2009
 */
package tentaflykt.editor.gui;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;

import tentaflykt.editor.EditorRes;
import tentaflykt.util.GUIUtil;

/**
 * Top menu for the editor.
 * @author Kristoffer Nilsson
 */
public class EditorTopMenu 
extends JMenuBar 
implements ActionListener {
  private static final long serialVersionUID = 9217073517562008751L;
  
  /*
   * GUI components
   */
  private JMenu topFile    = new JMenu(EditorRes.getString("Editor.Menu.File"));
  private JMenu topProject = new JMenu(EditorRes.getString("Editor.Menu.Project"));
  private JMenu topWindow  = new JMenu(EditorRes.getString("Editor.Menu.Window"));
  private JMenu subWindowToolbox = new JMenu(EditorRes.getString("Editor.Menu.Window.Toolbox"));
  private JMenu topHelp    = new JMenu(EditorRes.getString("Editor.Menu.Help"));
  
  private Component topFileItems[] = {
      new JMenuItem(EditorRes.getString("Editor.Menu.File.New")),
      new JMenuItem(EditorRes.getString("Editor.Menu.File.Open")),
      new JMenuItem(EditorRes.getString("Editor.Menu.File.Deploy")),
      new JSeparator(),
      new JMenuItem(EditorRes.getString("Editor.Menu.File.Exit"))
  };
  
  private Component topProjectItems[] = {
      new JMenuItem(EditorRes.getString("Editor.Menu.Project.FindWord")),
      new JSeparator(),
      new JMenuItem(EditorRes.getString("Editor.Menu.Project.Properties"))
  };
  
  private Component topWindowItems[] = {
      subWindowToolbox
  };
  
  private Component subWindowToolboxItems[] = {
      new JMenuItem(EditorRes.getString("Editor.Menu.Window.Toolbox.Reset"))
  };
  
  private Component topHelpItems[] = {
      new JMenuItem(EditorRes.getString("Editor.Menu.Help.About"))
  };

  /**
   * Default constructor.
   */
  public EditorTopMenu() {
    // File
    add(topFile);
    for (Component item : topFileItems) {
      topFile.add(item);
      if (item instanceof JMenuItem) {
        ((JMenuItem)item).addActionListener(this);
      }
    }

    // Project
    add(topProject);
    for (Component item : topProjectItems) {
      topProject.add(item);
      if (item instanceof JMenuItem) {
        ((JMenuItem)item).addActionListener(this);
      }
    }
    
    // Window
    add(topWindow);
    for (Component item : topWindowItems) {
      topWindow.add(item);
      if (item instanceof JMenuItem) {
        ((JMenuItem)item).addActionListener(this);
      }
    }
    
    subWindowToolbox.setHorizontalAlignment(SwingConstants.LEFT);
    for (Component item : subWindowToolboxItems) {
      subWindowToolbox.add(item);
      if (item instanceof JMenuItem) {
        ((JMenuItem)item).addActionListener(this);
      }
    }
    
    // Help
    add(topHelp);
    for (Component item : topHelpItems) {
      topHelp.add(item);
      if (item instanceof JMenuItem) {
        ((JMenuItem)item).addActionListener(this);
      }
    }
  }

  @Override
  public void actionPerformed(ActionEvent ae) {
    String actionCommand = ae.getActionCommand();
    
    if (actionCommand.equalsIgnoreCase(EditorRes.getString("Editor.Menu.File.Exit"))) {
      // exit program in a controlled manner
      GUIUtil.getMainFrame().tryExit();
    } else {
      System.out.println("Unknown menu action command: '" + actionCommand + "'");
    }
  }
}
